﻿using System;
using System.Linq;
using System.Linq.Dynamic;
using System.Web.Mvc;
using JqGridMvc.Models;
namespace JqGridMvc.Controllers
{
    [HandleError]
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            ViewBag.Message = "Welcome to ASP.NET MVC!";
           
            //ViewData["Message"] = "Welcome to ASP.NET MVC!";

            return View();
        }

        public ActionResult GridData(string sidx, string sord, int page, int rows)
        {
            int totalPages = 1; // we'll implement later
            int pageSize = rows;
            int totalRecords = 3; // implement later

            var jsonData = new
            {
                total = totalPages,
                page = page,
                records = totalRecords,
                rows = new[]{
                    new {id = 1, cell = new[] {"1", "-7", "Is this a good question?"}},
                    new {id = 2, cell = new[] {"2", "15", "Is this a blatant ripoff?"}},
                    new {id = 3, cell = new[] {"3", "23", "Why is the sky blue?"}}
                }
            };
            return Json(jsonData, JsonRequestBehavior.AllowGet);
        }

        public ActionResult LinqGridData(string sidx, string sord, int page, int rows)
        {
            using (HackOverflowDataContext context = new HackOverflowDataContext())
            {
                var jsonData = new
                {
                    total = 1, //todo: calculate
                    page = page,
                    records = context.Questions.Count(),
                    rows = (
                        from q in context.Questions
                        select new
                        {
                            i = q.Id,
                            cell = new string[] { q.Id.ToString(), q.Votes.ToString(), q.Title }
                        }).ToArray()
                };
                return Json(jsonData, JsonRequestBehavior.AllowGet);
            }

            
        }

        public ActionResult DynamicGridData(string sidx, string sord, int page, int rows)
        {
            using (HackOverflowDataContext context = new HackOverflowDataContext())
            {
                int pageIndex = Convert.ToInt32(page) - 1;
                int pageSize = rows;
                int totalRecords = context.Questions.Count();
                int totalPages = (int)Math.Ceiling((float)totalRecords / (float)pageSize);

                var questions = context.Questions.OrderBy(string.Format("{0} {1}",sidx, sord)).Skip(pageIndex * pageSize).Take(pageSize);

                var jsonData = new
                {
                    total = totalPages,
                    page,
                    records = totalRecords,
                    rows = (
                        from q in questions   //renamed q, better for templating
                        select new
                        {
                            i = q.Id,
                            cell = new string[] { q.Id.ToString(), q.Votes.ToString(), q.Title.ToString(),q.Body.ToString(),q.InsDate.ToString() }
                        }).ToArray()
                };
                return Json(jsonData, JsonRequestBehavior.AllowGet);
            }
            
        }

        public ActionResult About()
        {
            return View();
        }
    }
}
